let common_url = 'http://192.168.60.11:8080/'; //服务器地址

import React, {Component} from 'react';
import {View} from 'react-native';

export default class DataRepository extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <View></View>
        );
    }

    static fetchRequest(url, params = '',method) {
        console.log("params:" + JSON.stringify(params));
        let header = {
            'Accept': 'application/json',
            'Content-Type': 'application/json;charset=UTF-8'
        };

        return storage
            .load({key: 'token'})
            .then(ret => {
                header.Authorization = ret;
                if (params == '') { //如果网络请求中没有参数
                    return new Promise(function (resolve, reject) {
                        fetch(common_url + url, {
                            method: 'GET',
                            headers: header
                        }).then((response) => response.json()).then((responseData) => {
                            console.log('res:', url, responseData); //网络请求成功返回的数据
                            resolve(responseData);
                        }).catch((err) => {
                            console.log('err:', url, err); //网络请求失败返回的数据
                            reject(err);
                        });
                    });
                } else { //如果网络请求中带有参数
                    return new Promise(function (resolve, reject) {
                        fetch(common_url + url, {
                            method: 'POST',
                            headers: header,
                            body: JSON.stringify(params) //body参数，通常需要转换成字符串后服务器才能解析
                        }).then((response) => response.json()).then((responseData) => {
                            console.log('res:', url, responseData); //网络请求成功返回的数据
                            resolve(responseData);
                        }).catch((err) => {
                            console.log('err:', url, err); //网络请求失败返回的数据
                            reject(err);
                        });
                    });
                }
            })
            .catch(err => {
                console.warn(err.message);
            });
    }

    static fetchRequestWithOutToken(url, params = '') {
        console.log("params:" + JSON.stringify(params));
        let header = {
            'Accept': 'application/json',
            'Content-Type': 'application/json;charset=UTF-8'
        };
        if (params == '') { //如果网络请求中没有参数
            return new Promise(function (resolve, reject) {
                fetch(common_url + url, {
                    method: 'GET',
                    headers: header
                }).then((response) => response.json()).then((responseData) => {
                    console.log('res:', url, responseData); //网络请求成功返回的数据
                    resolve(responseData);
                }).catch((err) => {
                    console.log('err:', url, err); //网络请求失败返回的数据
                    reject(err);
                });
            });
        } else { //如果网络请求中带有参数
            return new Promise(function (resolve, reject) {
                fetch(common_url + url, {
                    method: 'POST',
                    headers: header,
                    body: JSON.stringify(params) //body参数，通常需要转换成字符串后服务器才能解析
                }).then((response) => response.json()).then((responseData) => {
                    console.log('res:', url, responseData); //网络请求成功返回的数据
                    resolve(responseData);
                }).catch((err) => {
                    console.log('err:', url, err); //网络请求失败返回的数据
                    reject(err);
                });
            });
        }
    }

}
